<?php

class search extends a_admin_controller {

    protected $users_found;
    protected $form_errors;
    protected $pag_links;
    protected $total_users_found;
    protected $user_search;

    public function __construct() {
        parent::__construct(true);
        $this->load->library("json");
        $this->load->library('session');
        $this->load->library('pagination');
        $this->form_errors = "";
        $this->user_search=NULL;
        $this->reset_session();
    }

    public function batch_assign(){
        $this->assign("users", $this->users_found);
        $this->assign("pag_links", $this->pag_links);
        $this->assign("form_errors", $this->form_errors);
        $this->assign("total_users_found", $this->total_users_found);
        $this->assign("user_search", $this->user_search);

    }

    public function index()
    {
    }

    public function auto()
    {
        $search_list = new user_service_l_user();
        $test = array(
                "fname" => $_POST['queryString'],
                "lname" => "",
                "username" => "",
                "email" => "",
                "province" => "",
                "country" => ""
        );

        $autocomplete = $search_list->autocomplete($test);
        
        echo $autocomplete;
        
        die();
    }

    public function process()
    {
        $data=array();

        $search_list = new search_processor_l_user;
//        if($this->is_session()==0)
//        {
//            $this->set_session();
//        }

        $this->set_session();
        
        $this->user_search = $this->get_session();

        $this->total_users_found = $search_list->count_users($this->user_search);
        $this->users_found = $search_list->list_product($this->user_search, //$start_index, $num_records, $sort_column, $sort_dir, $search_filter);
                $this->user_search["start_index"], $this->user_search["num_records"], $this->user_search["sort_column"], $this->user_search["sort_dir"], $this->user_search["search_dir"]);

        $data['aaData'] = $this->users_found;
        $data['iTotalRecords'] = $this->total_users_found;
        $data['iTotalDisplayRecords'] = $this->total_users_found;
        $data['dir'] = 'asc';

        $from = array('\n', '\\/');
        $to   = array('', '/');

        echo str_replace($from, $to, $this->json->encode($data));
        die();
        $this->view("search");
    }

    private function set_session()
    {
        //adding to session
        $search = array(
               'sess_search_fname'  => trim($this->input->post('fname')),
               'sess_search_lname'  => trim($this->input->post('lname')),
               'sess_search_username'  => trim($this->input->post('username')),
               'sess_search_email'     => trim($this->input->post('email')),
               'sess_search_province' => trim($this->input->post('billing_province')),
               'sess_search_country' => trim($this->input->post('country')),

               'sess_start_index'   => $_POST['iDisplayStart'],
               'sess_num_records'   => $_POST['iDisplayLength'],
               'sess_sort_column'   => $_POST['iSortCol_0'],
               'sess_sort_dir'   => $_POST['sSortDir_0'],
               'sess_search_dir'   => $_POST['sSearch']
           );

        $this->session->set_userdata($search);
    }

    private function get_session()
    {
        $list = array(
                "fname" => $this->session->userdata('sess_search_fname'),
                "lname" => $this->session->userdata('sess_search_lname'),
                "username" => $this->session->userdata('sess_search_username'),
                "email" => $this->session->userdata('sess_search_email'),
                "province" => $this->session->userdata('sess_search_province'),
                "country" => $this->session->userdata('sess_search_country'),

                "start_index" => strcmp($this->session->userdata('sess_start_index'),"") ? $this->session->userdata('sess_start_index') : 0,
                "num_records" => $this->session->userdata('sess_num_records'),
                "sort_column" => strcmp($this->session->userdata('sess_sort_column'),"") ? $this->session->userdata('sess_sort_column') : 0,
                "sort_dir" => $this->session->userdata('sess_sort_dir'),
                "search_dir" => $this->session->userdata('sess_search_dir')
        );

        return $list;
    }

    public function destroy_user_search_session()
    {
        $this->session->unset_userdata('sess_search_username');
        $this->session->unset_userdata('sess_search_email');
        $this->session->unset_userdata('sess_search_province');
        $this->session->unset_userdata('sess_search_country');
        
        $this->session->unset_userdata('sess_start_index');
        $this->session->unset_userdata('sess_num_records');
        $this->session->unset_userdata('sess_sort_column');
        $this->session->unset_userdata('sess_sort_dir');
        $this->session->unset_userdata('sess_search_dir');
    }

    public function set_pagination_config($users_count, $limit)
    {
        $config['base_url'] = base_url()."index.php/user/admin/search/process/";
        $config['total_rows'] = $users_count;
        $config['per_page'] = $limit;

        $this->pagination->initialize($config);
        return $this->pagination->create_links();
    }

    public function reset_session()
    {
        if($this->input->post('reset'))
            $this->destroy_user_search_session();
    }

    protected function is_session()
    {
        $session =  $this->session->userdata('sess_search_fname').
                    $this->session->userdata('sess_search_lname').
                    $this->session->userdata('sess_search_username').
                    $this->session->userdata('sess_search_email').
                    $this->session->userdata('sess_search_province').
                    $this->session->userdata('sess_search_country');

        return strcasecmp($session, "")==0 ? 0 : 1;
    }

}

?>